package com.yumaolin.deepunderstand.leetcode.algorithm.simple;

/**
 * 121. 买卖股票的最佳时机
 */
public class MaxProfit {
    public static int maxProfit(int[] prices) {
        int len = prices.length;
        if(len < 2){
            return 0;
        }
        int minVal = Integer.MAX_VALUE;
        int maxVal = 0;
        for (int price : prices) {
            if (price < minVal) {
                minVal = price;
            } else if (price - minVal > maxVal) {
                maxVal = price - minVal;
            }
        }
        return maxVal;
    }

    public static void main(String[] args) {
        int[] prices = new int[]{7,1,5,3,6,4};
        System.out.println(maxProfit(prices));
    }
}
